#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:sort_scores_to_file.py
# author:张世航
# datetime:2024/11/12 22:43
# software: PyCharm
"""
练习:   一个文件内容的如下,请按照行读取文件的内容,  将分数排序后输出到另外一个文件中:
            姓名      学号      分数
            张三      101         78
            李四      102         87
            王五       103        83

"""
# import module your need

# 读取文件内容，按分数排序，并输出到新文件
with open("scores.txt", "r", encoding="utf-8") as infile, open("sorted_scores.txt", "w", encoding="utf-8") as outfile:
    lines = infile.readlines()
    header = lines[0]
    outfile.write(header)
    scores = sorted(lines[2:], key=lambda x: int(x.split()[-1]))
    for score in scores:
        outfile.write(score)